﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using FellowshipOne.API.Model;

namespace FellowshipOne.API.Events.Model
{
    [Serializable]
    [XmlRoot("attendanceSummary")]
    public class AttendanceSummaryEdit : ApiModel
    {
        
    }

    [Serializable]
    [XmlRoot("person")]
    public class PersonEdit : ApiModel
    {

    }

    [Serializable]
    [XmlRoot("attendance")]
    public class Attendance : ApiModel
    {
        private AttendanceSummaryEdit _attendanceSummary = new AttendanceSummaryEdit();
        private PersonEdit _person = new PersonEdit();
        private DateTime _attendanceDate;
        private bool _isPresent;
        private string _checkInDateTimeString = String.Empty;
        private string _checkOutDateTimeString = String.Empty;
        private string _updatedDateString = string.Empty;
        private DateTime _createdDate;

        [XmlElement("attendanceSummary")]
        public AttendanceSummaryEdit AttendanceSummary
        {
            get { return _attendanceSummary; }
            set { _attendanceSummary = value; }
        }

        [XmlElement("person")]
        public PersonEdit Person
        {
            get { return _person; }
            set { _person = value; }
        }

        [XmlElement("attendanceDate")]
        public DateTime AttendanceDate
        {
            get { return _attendanceDate; }
            set { _attendanceDate = value; }
        }

        [XmlElement("isPresent")]
        public bool IsPresent
        {
            get { return _isPresent; }
            set { _isPresent = value; }
        }

        [XmlIgnore]
        public DateTime? CheckInDateTime
        {
            get
            {
                DateTime dt = DateTime.MinValue;

                if (DateTime.TryParse(_checkInDateTimeString, out dt))
                {
                    return dt;
                }

                return null;
            }
            set
            {
                if (value.HasValue)
                {
                    _checkInDateTimeString = value.Value.ToString();
                }
                else
                {
                    _checkInDateTimeString = string.Empty;
                }
            }
        }

        [XmlElement("checkinDateTime")]
        public String CheckInDateTimeString
        {
            get
            {
                if (!string.IsNullOrEmpty(_checkInDateTimeString))
                {
                    DateTime dt = DateTime.Now;

                    if (DateTime.TryParse(_checkInDateTimeString, out dt))
                    {
                        _checkInDateTimeString = dt.ToString("s");
                    }
                }

                return _checkInDateTimeString;
            }
            set
            {
                if (value != null)
                {
                    _checkInDateTimeString = value;
                }
            }
        }

        [XmlIgnore]
        public DateTime? CheckOutDateTime
        {
            get
            {
                DateTime dt = DateTime.MinValue;

                if (DateTime.TryParse(_checkOutDateTimeString, out dt))
                {
                    return dt;
                }

                return null;
            }
            set
            {
                if (value.HasValue)
                {
                    _checkOutDateTimeString = value.Value.ToString();
                }
                else
                {
                    _checkOutDateTimeString = string.Empty;
                }
            }
        }

        [XmlElement("checkoutDateTime")]
        public String CheckOutDateTimeString
        {
            get
            {
                if (!string.IsNullOrEmpty(_checkOutDateTimeString))
                {
                    DateTime dt = DateTime.Now;

                    if (DateTime.TryParse(_checkOutDateTimeString, out dt))
                    {
                        _checkOutDateTimeString = dt.ToString("s");
                    }
                }

                return _checkOutDateTimeString;
            }
            set
            {
                if (value != null)
                {
                    _checkOutDateTimeString = value;
                }
            }
        }

        [XmlElement("createdDate")]
        public DateTime CreatedDate
        {
            get { return _createdDate; }
            set { _createdDate = value; }
        }

        [XmlIgnore]
        public DateTime? UpdatedDate
        {
            get
            {
                DateTime updateDate = DateTime.MinValue;

                if (DateTime.TryParse(_updatedDateString, out updateDate))
                {
                    return updateDate;
                }

                return null;
            }
            set
            {
                if (value.HasValue)
                {
                    _updatedDateString = value.Value.ToString();
                }
                else
                {
                    _updatedDateString = string.Empty;
                }
            }
        }

        [XmlElement("updatedDate")]
        public string UpdatedDateString
        {
            get
            {
                if (!string.IsNullOrEmpty(_updatedDateString))
                {
                    DateTime dt = DateTime.Now;

                    if (DateTime.TryParse(_updatedDateString, out dt))
                    {
                        _updatedDateString = dt.ToString("s");
                    }
                }

                return _updatedDateString;
            }
            set
            {
                if (value != null)
                {
                    _updatedDateString = value;
                }
            }
        }
    }

    [Serializable]
    [XmlRoot("attendances")]
    public class AttendanceCollection : Collection<Attendance>
    {
        public AttendanceCollection()
        {
        }

        public AttendanceCollection(List<Attendance> attendances)
        {
            if (attendances != null)
            {
                this.AddRange(attendances);
            }
        }

    }
}
